package com.bjqg.bjqg01.bjqg.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.bjqg.bjqg01.bjqg.R;
import com.bjqg.bjqg01.bjqg.bean.Message;

import java.util.List;


/**
 * Created by bjqg01 on 2017/10/24.
 */
public class MessageAdapter extends BaseAdapter {

    private List<Message> listStr;
    private LayoutInflater inflater;

    public MessageAdapter(Context context, List<Message> listStr) {
        this.listStr = listStr;
        this.inflater = LayoutInflater.from(context);
    }


    @Override
    public int getCount() {
        return listStr.size();
    }

    @Override
    public Object getItem(int position) {
        return listStr.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = inflater.inflate(R.layout.item_message, null);
            holder.userInage = (ImageView) convertView.findViewById(R.id.userInage);
            holder.tv_str = (TextView) convertView.findViewById(R.id.tv_str);
            holder.mTime = (TextView) convertView.findViewById(R.id.time);
            holder.talk = (ImageView) convertView.findViewById(R.id.talk);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
            if (listStr.size() > 0 && !holder.tv_str.getText().toString().trim().equals("")) {
                holder.tv_str.setVisibility(View.VISIBLE);
                holder.talk.setVisibility(View.VISIBLE);
                holder.userInage.setVisibility(View.VISIBLE);
                holder.mTime.setVisibility(View.VISIBLE);
            } else {
                holder.tv_str.setVisibility(View.INVISIBLE);
                holder.talk.setVisibility(View.INVISIBLE);
                holder.userInage.setVisibility(View.INVISIBLE);
                holder.mTime.setVisibility(View.INVISIBLE);
            }

        }
//        holder.userInage.setImageDrawable(listStr.get(position).getIcon());
        holder.tv_str.setText(listStr.get(position).getContent());
        holder.mTime.setText(listStr.get(position).getTimeContent());
        return convertView;
    }


    class ViewHolder {

        ImageView talk;
        TextView tv_str;
        ImageView userInage;
        TextView mTime;

    }
}
